/**
 * Created by Administrator on 2017/7/15.
 */
//var compareArray = ["OneMap","OneMap15","NavigationMap"];
var compareArray = layerUtil.compareArray;

function initSlider() {
    var value = $(".time-slider").css("display");

    var len = compareArray.length;
    var map = mapObject.getInstance().getMap();


    if(value=="none"){

        $(".time-slider").css("display","block");

        map.getLayersByName(compareArray[0])[0].setVisibility(true);
        map.getLayersByName(compareArray[1])[0].setVisibility(true);
        map.getLayersByName(compareArray[2])[0].setVisibility(true);

        map.getLayersByName(compareArray[0])[0].setOpacity(1);
        map.getLayersByName(compareArray[1])[0].setOpacity(1);
        map.getLayersByName(compareArray[2])[0].setOpacity(1);
    }else{
        $(".time-slider").css("display","none");
        map.getLayersByName(compareArray[0])[0].setVisibility(false);
        map.getLayersByName(compareArray[1])[0].setVisibility(false);

        map.getLayersByName(compareArray[2])[0].setOpacity(1);

        return;
		
    }
	//初始就在最末尾
    createSlider(len,200);
	$(".slider").mousedown(function(e){
		//判断是不是左键1是左键
		if(1 != e.which){
			return;
		}
		$("body").mouseup(function(){
			//四舍五入
			//假设有3，step就是200，此时为150,每期100
			var _length = compareArray.length;
			//console.log($(".ui-slider-pip-selected").find(".ui-slider-label").attr("data-value"));
			var value = $(".ui-slider-pip-selected").find(".ui-slider-label").attr("data-value");
			//console.log(value/100);
			var round = Math.round(value/100);
			//console.log(round);
			createSlider(_length,round*100);
			var wl = compareArray[round];
			
			for(var i=0;i<_length;i++){
				if(i<=round){
					map.getLayersByName(compareArray[i])[0].setOpacity(1);
				}else{
					map.getLayersByName(compareArray[i])[0].setOpacity(0);
				}
				
			}
			//map.getLayersByName(wl)[0].setOpacity(1);
			$("body").off("mouseup");
		});
		
	});
}

function createSlider(len,initvalue){
	$(".slider").off("slide");
	$(".slider").off("slidechange");
	var map = mapObject.getInstance().getMap();
	$(".slider").slider(
			{ 
		        min: 0, 
		        max: (len-1)*100, 
		        value:initvalue , 
		        step: 1 //单位大小1
		    }
		)
		.slider("pips", {
			//handle: true,  
		    //pips: false,
			//first:false,
			//last:false,
			rest: false,//不现实刻度
			labels:replaceLabels(compareArray)
			}
		
		).on("slide", function(e,ui) {
	    		//当前要加载的是哪个图层
			//console.log(ui.value);
	    		var whichlayer = compareArray[parseInt(ui.value/100)+1];
	    		if(ui.value<=(len-1)*100&&ui.value>0){
	    			 if(ui.value==(len-1)*100){
			    		  return;
			    	   }
			    	  // $(".ui-slider-tip").text(comparearray[i]);
			    	   $(ui.handle).find(".ui-slider-tip").css("display","none");
			    	   map.getLayersByName(whichlayer)[0].setOpacity(ui.value/100-Math.floor(ui.value/100));
	    		 }else if(ui.value==0){
			    	   $(ui.handle).find(".ui-slider-tip").css("display","none");
			     }
	       
		})
	    .on("slidechange", function(e,ui) {//滑块值变化事件
	    	//当前要加载的是哪个图层
			var whichlayer = compareArray[parseInt(ui.value/100)+1];
			if(ui.value<=(len-1)*100&&ui.value>0){
				 if(ui.value==(len-1)*100){
		    		  return;
		    	   }
		    	  // $(".ui-slider-tip").text(comparearray[i]);
		    	   $(ui.handle).find(".ui-slider-tip").css("display","none");
		    	   map.getLayersByName(whichlayer)[0].setOpacity(ui.value/100-Math.floor(ui.value/100));
			 }else if(ui.value==0){
		    	   $(ui.handle).find(".ui-slider-tip").css("display","none");
		     } 
	    });
	//刻度
	var labels = $(".ui-slider-label");
	
	for (var i = 0; i < labels.length; i++) {
		
		if($(labels[i]).html()!=""){
			$(labels[i]).parent().css("display","block");
			$(labels[i]).css("display","block");

		}
	}
	
}


function replaceLabels(arg){
    var labels=[];

    for (var i = 0; i <= (arg.length-1)*100; i++) {
        if(i===0){
            labels[i]= initTime("OneMap")
        }else if(i===100){
            labels[i]= initTime("OneMap15")
        }else if(i===200){
            labels[i]=initTime("NavigationMap");
        }else{
            labels[i]="";
        }
    }
    //console.log(labels);
    return labels;
}
function initTime(layername){
    var time;
    var map = mapObject.getInstance().getMap();
    var lonLat = map.getCenter();
    $.ajax({
        type: 'GET',
        url: "http://123.56.192.226:7090/onemap/time" ,
        data: {
        "lon":lonLat.lon,
            "lat":lonLat.lat,
            "layername":layername

		} ,
		async : false,
        success: function(data){

        time=data;

    },
    error:function(data){
        alert("获取时间失败");
    }
});
    return time;
}
function getTimeFromShp(){
    changeTime("OneMap");
    changeTime("OneMap15");
    changeTime("NavigationMap");
}
function changeTime(layername){
    //alert(layername);
    var map = mapObject.getInstance().getMap();
    var zoom = map.getZoom();
    if(zoom<11){
        return;
    }
    var lonLat = map.getCenter()
	if("NavigationMap"==layername){
		$.ajax({
			type: 'GET',
			url: "http://60.194.14.226:7090/dateTiff" ,
			data: {
				"lon":lonLat.lon,
				"lat":lonLat.lat
				
			} ,
			//async : false,
			success: function(data){
				$("[data-value|='200']").html(data);
			},
			error:function(data){

			}
		});
	}else{
		$.ajax({
			type: 'GET',
			url: "http://123.56.192.226:7090/onemap/time" ,
			data: {
				"lon":lonLat.lon,
				"lat":lonLat.lat,
				"layername":layername

			} ,
			//async : false,
			success: function(data){
				//eval(data)
				//alert(data);
				if("OneMap"==layername){

					$("[data-value|='0']").html(data);

				}else if("OneMap15"==layername){
					$("[data-value|='100']").html(data);
				}

			},
			error:function(data){

			}


    });
	}
    
}